package com.aramis.mydytt.ui.topic

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.aramis.mydytt.R
import com.aramis.mydytt.ui.bean.MovieSpecialBean
import com.facebook.drawee.view.SimpleDraweeView

/**
 * Description: MainTopicListAdapter
 * @author lizhidan
 * Created: 2021/5/21
 */
class MainTopicListAdapter : RecyclerView.Adapter<MainTopicListAdapter.ViewHolder>() {
    private val dataList = mutableListOf<MovieSpecialBean>()
    var onItemClickListener: ((MovieSpecialBean) -> Unit)? = null

    fun setData(list: List<MovieSpecialBean>?) {
        dataList.clear()
        if (list != null) {
            dataList.addAll(list)
        }
        notifyDataSetChanged()
    }

    fun addData(list: List<MovieSpecialBean>?) {
        if (list != null) {
            dataList.addAll(list)
        }
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_main_topic, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        dataList[position].apply {
            holder.imageView.setImageURI(this.cover)
            holder.textView.text = this.title
            holder.itemView.setOnClickListener { onItemClickListener?.invoke(this) }
        }
    }

    override fun getItemCount(): Int = dataList.size

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val imageView: SimpleDraweeView = itemView.findViewById(R.id.image_topic)
        val textView: TextView = itemView.findViewById(R.id.text_topic)
    }
}